/**
 * @author Helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.CategoriesDao) SALE.dao.local.CategoriesDao = function(){
	var categoriesDoc;
}

SALE.dao.local.CategoriesDao.prototype = {	
		
		initCategoriesDoc: function() {
			if(!this.categoriesDoc) {
				this.categoriesDoc = LocalXMLFileUtil.getCategoryDoc();
			}
			return this.categoriesDoc;
		},
		
		saveCategoriesDoc : function() {
			LocalXMLFileUtil.saveXML(this.categoriesDoc,LocalXMLFileUtil.getCategoryFile());
		},
		
		insertCategoryInfo : function(categoryJson) {
			var root = this.categoriesDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			categoryJson["id"] = id;
			root.setAttribute("maxid",id);
			var categoryNode = this.categoriesDoc.createElement("category");
			
			this.updateCategory(categoryJson, categoryNode);			
			root.appendChild(categoryNode);
			
			this.saveCategoriesDoc();
		},
		
		modifyCategoryInfo : function(categoryJson) {
			var categoryNode = this.searchCategoryByID(categoryJson.id);
			
			this.updateCategory(categoryJson, categoryNode);
			
			this.saveCategoriesDoc();
		},
		
		deleteCategoryInfo : function(id) {
			var categoryNode = this.searchCategoryByID(id);
			var root = this.categoriesDoc.documentElement;
			if(categoryNode != null) {
				root.removeChild(categoryNode);
			}			
		},
				
		updateCategory : function(categoryJosn, categoryNode) {
			
			for(var attri in categoryJosn) {	
				
				if(attri.indexOf("old") != 0 && categoryJosn[attri] && categoryJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, categoryNode);
					if(attriNodes != null && attriNodes.length > 0) {
						categoryNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.categoriesDoc.createElement(attri);
					var contextNode = this.categoriesDoc.createCDATASection(categoryJosn[attri]);
					attriNode.appendChild(contextNode);
					categoryNode.appendChild(attriNode);
				}
			}
		},
		
		searchCategoryByID : function(id) {
			var root = this.categoriesDoc.documentElement;
			var objArray = Ext.query("/category:has(id:nodeValue(" + id+ "))", root);
			var result;
			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchCategoryJsonByID : function(id) {
			
			var resultNode = this.searchCategoryByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		buildCategoriesJson: function() {
			var root = this.initCategoriesDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/category", root);
				var categoriesJson = {categories: []};
				
				Ext.each(objArray, function(category) {
					var children = category.childNodes;
					var categoryJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!categoryJson[child.nodeName] && child.childNodes[0] != null) {
								categoryJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					categoriesJson.categories.push(categoryJson);
				});
				
				return categoriesJson;
			}
		}	
}
